package liyasong.tool.crawler;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.horrabin.horrorss.RssChannelBean;
import org.horrabin.horrorss.RssFeed;
import org.horrabin.horrorss.RssItemBean;
import org.horrabin.horrorss.RssParser;

public class BlogRssCrawler {

//	private final String charset = "utf-8";
	private List<String> rssLinks;
	private List<RssItemBean> articles;

	private final String writeDir = "resources";
	
	public BlogRssCrawler(String rssLink) {
		rssLinks = new ArrayList<>();
		rssLinks.add(rssLink);
		articles = new ArrayList<>();
	}

	public BlogRssCrawler(List<String> rssLinks) {
		this.rssLinks = rssLinks;
		articles = new ArrayList<>();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String rssLink = "http://firecacada.blog.163.com/rss/";
		BlogRssCrawler brc = new BlogRssCrawler(rssLink);
		brc.crawl();
		brc.writeResult();
		System.out.println("over");

	}

	public List<RssItemBean> crawl() {
		RssParser rssParser = new RssParser(); 
		for (String rsslink : rssLinks) {
			try {
				System.out.println("before feed");
				RssFeed feed = rssParser.load(rsslink);
				System.out.println("after feed");
				RssChannelBean channel = feed.getChannel();
		        System.out.println("Feed Title: " + channel.getTitle());
				articles.addAll(feed.getItems());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return articles;
	}
	
	public List<RssItemBean> getArticles() {
		return articles;
	}
	
	public void writeResult() {
		System.out.println(articles.size());
		for (RssItemBean article : articles) {
			String dirName = article.getAuthor();
			String path	= writeDir + "\\" + dirName;
			File dir = new File(path);
			if (!dir.exists()) {
				dir.mkdir();
			}
			File file = new File(path + "\\" + article.getTitle());
			try {
				FileWriter fw = new FileWriter(file);
				String lineRegex = "<br[ /]*>";
				String htmlRegex = "<[^>]*>";		
				String description = article.getDescription().replaceAll(lineRegex, "\n").replaceAll(htmlRegex, "");
				fw.write(description);
				fw.flush();
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
